package net.tarine.gastronomi.client.tables;

import java.util.ArrayList;
import java.util.List;

import net.tarine.gastronomi.client.HistoryMgr;
import net.tarine.gastronomi.client.IRefreshable;
import net.tarine.gastronomi.client.UiSingleton;
import net.tarine.gastronomi.client.services.ForumService;
import net.tarine.gastronomi.client.services.ForumServiceAsync;
import net.tarine.gastronomi.shared.AppConst;
import net.tarine.gastronomi.shared.to.ForumMessageTo;
import net.tarine.gastronomi.shared.to.ForumThreadTo;
import net.tarine.gastronomi.shared.to.GasTo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;

public class ForumThreadTable extends PagingTable<ForumThreadTo> implements IRefreshable {
	
	private static ForumServiceAsync forumService = GWT.create(ForumService.class);
	private DateTimeFormat dtf = DateTimeFormat.getFormat("HH:mm dd/MM/yyyy");
	
	private AsyncCallback<List<ForumThreadTo>> callback = new AsyncCallback<List<ForumThreadTo>>() {
		@Override
		public void onFailure(Throwable caught) {
			UiSingleton.get().addError(caught);
			setTableRows(new ArrayList<ForumThreadTo>());
		}
		@Override
		public void onSuccess(List<ForumThreadTo> result) {
			setTableRows(result);
		}
	};
	
	public ForumThreadTable(DataModel<ForumThreadTo> model) {
		super(model);
		drawPage(0);
	}

	@Override
	protected void drawPage(int page) {
		getInnerTable().removeAllRows();
		getInnerTable().setHTML(0, 0, "<img src='img/ajax-loader-small.gif' style='vertical-align:middle' />&nbsp;" +
				"Caricamento in corso...");
		getModel().find(page*AppConst.PAGING_ROWS,
				AppConst.PAGING_ROWS,
				callback);
	}
	
	@Override
	public void refresh() {
		drawPage(0);
	}
	
	@Override
	protected void addTableRow(int rowNum, ForumThreadTo rowObj) {
		final ForumThreadTo rowFinal = rowObj;
		List<ForumMessageTo> ml = rowObj.getForumMessagesToList();
		if (ml == null) return;
		if (ml.size() == 0) return;
		ForumMessageTo first = ml.get(0);
		ForumMessageTo last = ml.get(ml.size()-1);
		
		// Set the data in the current row
		Hyperlink rowLink = new Hyperlink();
		rowLink.setHTML("<b>"+rowObj.getTitle()+"</b> ("+ml.size()+")");
		rowLink.setTargetHistoryToken(HistoryMgr.FORUM_THREAD_EDIT+HistoryMgr.SEPARATOR_TOKEN+
				HistoryMgr.PARAM_ID+HistoryMgr.SEPARATOR_VALUES+rowObj.getId());
		getInnerTable().setWidget(rowNum, 0, rowLink);

		getInnerTable().setHTML(rowNum, 1, first.getGasUserTo().getNickname()+"<br/>"+
				dtf.format(first.getModifiedDt()));
		getInnerTable().setHTML(rowNum, 2, last.getGasUserTo().getNickname()+"<br/>"+
				dtf.format(last.getModifiedDt()));
		Image trashImg = new Image("img/trash.png");
		trashImg.setTitle("elimina");
		trashImg.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent arg0) {
				confirmAndDelete(rowFinal.getId());
			}
		});
		getInnerTable().setWidget(rowNum, 3, trashImg);
		rowNum += 1;
	}

	@Override
	protected void addHeader() {
		// Set the data in the current row
		getInnerTable().setHTML(0, 0, "Titolo");
		getInnerTable().setHTML(0, 1, "Creato da");
		getInnerTable().setHTML(0, 2, "Ultimo messaggio");
	}
	
	private void confirmAndDelete(String idUtente) {
		boolean confirm = Window.confirm("Vuoi veramente cancellare l'utente?");
		if (confirm) {
			delete(idUtente);
		}
	}
	
	public void delete(String threadId) {
		AsyncCallback<Boolean> deleteCallback = new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				UiSingleton.get().addError(caught);
			}
			@Override
			public void onSuccess(Boolean result) {
				getModel().find(0, AppConst.PAGING_ROWS, callback);
			}
		};
		forumService.deleteForumThread(threadId, deleteCallback);
	}
	
	
	
	
	//Inner classes
	
	
	
	public static class ForumThreadModel implements DataModel<ForumThreadTo> {
		
		private GasTo gas = null;
		
		public ForumThreadModel(GasTo gas) {
			this.gas = gas;
		}

		@Override
		public void find(int offset, int pageSize, AsyncCallback<List<ForumThreadTo>> callback) {
			forumService.findForumThreads(gas.getId(), offset, pageSize, callback);
		}
	}
	
}
